package com.lancoo.cvas.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lancoo.cvas.arrangecourse.ArrangeCourseService;
import com.lancoo.cvas.aspect.LogAnnotation;
import com.lancoo.cvas.entity.Schedule;
import com.lancoo.cvas.entity.ScheduleConflictInfo;
import com.lancoo.cvas.entity.TaskInfo;
import com.lancoo.cvas.exception.CapacityException;
import com.lancoo.cvas.pojo.common.PageInfo;
import com.lancoo.cvas.pojo.dto.*;
import com.lancoo.cvas.pojo.vo.*;
import com.lancoo.cvas.service.ScheduleService;
import com.lancoo.cvas.service.TaskInfoService;
import com.lancoo.cvas.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: liufeng
 * @CreateTime: 2023-12-09  16:45
 * @Description:
 */

@Api(tags = "编排课表")
@RestController
@RequestMapping("/api/v5.0.1/web/arrange")
public class CourseArrangeController {

    @Resource
    private ScheduleService scheduleService;
    @Resource
    private TaskInfoService taskInfoService;

    @Resource
    private ArrangeCourseService arrangeCourseService;

    @ApiOperation("获取课程教学班列表")
    @GetMapping(value = "/list/course")
    @LogAnnotation(operationType = "3", operationContent = "获取课程教学班列表")
    public Result<PageInfo<TeachingClassCourseDto>> getTeachingClassCourse(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                                    @ApiParam(value = "排课状态 1已排（正常）、2已排（未满足规则）、3已排（冲突）、4 待排", required = false) @RequestParam(required = false) Integer scheduleState,
                                                                                    @ApiParam(value = "年级", required = false) @RequestParam(required = false) String gradeId,
                                                                                    @ApiParam(value = "专业", required = false) @RequestParam(required = false) String majorId,
                                                                                    @ApiParam(value = "搜索关键字", required = false) @RequestParam(required = false) String keyWord,
                                                                                    @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                                                    @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {
        Page<TeachingClassCourseDto> page = new Page<>(currentPage, pageSize);
        page.setOptimizeCountSql(false);
//        return Result.ofSuccess(scheduleService.getTeachingClassCoursePage(taskId, page, scheduleState, gradeId, majorId, keyword));
        return Result.ofSuccess(scheduleService.getTeachingClassCourse(taskId, page, scheduleState, gradeId, majorId, keyWord));
    }



    @ApiOperation("获取该教学班冲突")
    @GetMapping(value = "getScheduleConflictInfo")
    @LogAnnotation(operationType = "3", operationContent = "获取该教学班冲突")
    public Result<List<ScheduleConflictInfo>> getScheduleConflictInfo(@ApiParam(value = "教学班id", required = true) @RequestParam(required = true) Long teachingClassId,
                                                                      @ApiParam(value = "授课方式", required = true) @RequestParam(required = true) Integer hourType) {
        return Result.ofSuccess(scheduleService.getScheduleConflictInfo(teachingClassId, hourType));
    }

    @ApiOperation("修改锁定状态")
    @PutMapping(value = "/updateLock")
    @LogAnnotation(operationType = "2", operationContent = "修改锁定状态")
    public Result<Boolean> updateLock(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                      @ApiParam(value = "1解锁  2锁定", required = true) @RequestParam Integer isLocked,
                                      @ApiParam(value = "教学班id", required = true) @RequestBody LockDto locks) {
        return Result.ofSuccess(scheduleService.updateLock(taskId, isLocked, locks));
    }

    @ApiOperation("获取锁定的课程教学班数量")
    @GetMapping(value = "/locks")
    @LogAnnotation(operationType = "3", operationContent = "获取锁定的课程教学班数量")
    public Result<Long> getAllLockedTeachingClass(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return Result.ofSuccess();
    }

    @ApiOperation("锁定所有正常教学班")
    @PutMapping(value = "/lockedNormal")
    @LogAnnotation(operationType = "2", operationContent = "锁定所有正常教学班")
    public Result<Boolean> lockedNormal(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return Result.ofSuccess(scheduleService.allNormalLocal(taskId));
    }

    @ApiOperation("解锁锁所有正常教学班")
    @PutMapping(value = "/unlockedNormal")
    @LogAnnotation(operationType = "2", operationContent = "解锁锁所有正常教学班")
    public Result<Boolean> unlockedNormal(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return Result.ofSuccess(scheduleService.unlockedNormal(taskId));
    }

    @ApiOperation("是否锁定所有正常教学班")
    @GetMapping(value = "/isLockedNormal")
    @LogAnnotation(operationType = "3", operationContent = "查看是否锁定所有正常教学班")
    public Result<Integer> isLockedNormal(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return Result.ofSuccess(scheduleService.isLockedNormal(taskId));
    }

    @ApiOperation("获取课表编排统计信息")
    @GetMapping(value = "/arrangeStatistics")
    @LogAnnotation(operationType = "3", operationContent = "获取课表编排统计信息")
    public Result<StatisticsArrange> getPublishStatisticsArrange(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                 @ApiParam(value = "学校id", required = true) @RequestParam String schoolId) {
        return Result.ofSuccess(scheduleService.getPublishStatisticsArrangeV2(taskId, schoolId));
    }

    @ApiOperation("自动排课")
    @GetMapping(value = "/autoArrangement")
    @LogAnnotation(operationType = "2", operationContent = "自动排课")
    public Result<List<ScheduleConflictInfo>> courseArrangement(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return Result.ofSuccess(scheduleService.autoArrangement(taskId));
    }

    @ApiOperation("需要增加教室信息")
    @GetMapping(value = "/requireRoom")
    @LogAnnotation(operationType = "3", operationContent = "查看需要增加教室信息")
    public Result<RoomRequire> requireRoom(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                           @ApiParam(value = "学校id", required = true) @RequestParam String schoolId,
                                           @ApiParam(value = "学期", required = true) @RequestParam String term,
                                           @ApiParam(value = "教室类型", required = false) @RequestParam(required = false) Integer roomType) {
        return Result.ofSuccess(scheduleService.requireRoom(taskId,schoolId,term,roomType));
    }

    @ApiOperation("锁定的班级信息")
    @GetMapping(value = "/classStatistics")
    @LogAnnotation(operationType = "3", operationContent = "查看锁定的班级信息")
    public Result<int[]> classStatistics(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                           @ApiParam(value = "学校id", required = true) @RequestParam String schoolId,
                                           @ApiParam(value = "学期", required = true) @RequestParam String term) {
        return Result.ofSuccess(scheduleService.classStatistics(taskId,schoolId,term));
    }

    @ApiOperation("获取一个教学班信息")
    @GetMapping(value = "/getById")
    @LogAnnotation(operationType = "3", operationContent = "获取一个教学班信息")
    public Result<TeachingClassCourseDto> getTeachingClassById(@ApiParam(value = "教学班id", required = true) @RequestParam Long teachingClassId) {
        return Result.ofSuccess();
    }

    @ApiOperation("生成教学班课表初始数据")
    @PostMapping(value = "/generateTeachingClassUnit")
    @LogAnnotation(operationType = "1", operationContent = "生成教学班课表初始数据")
    public Result<List<Schedule>> generateTeachingClassUnit(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                            @ApiParam(value = "调整课表", required = true) @RequestBody List<GenerateTeachingClassUnitInput> generateTeachingClassUnits) {
        return Result.ofSuccess(scheduleService.generateTeachingClassUnit(taskId, generateTeachingClassUnits));
    }

    @ApiOperation("未排课状态教学班生成教学班课表初始数据")
    @PostMapping(value = "/allToTeachingClassUnit")
    @LogAnnotation(operationType = "1", operationContent = "未排课状态教学班生成教学班课表初始数据")
    public Result<Boolean> allToTeachingClassUnit(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return Result.ofSuccess(scheduleService.allToTeachingClassUnit(taskId));
    }

    @ApiOperation("获取调整场地节次 列表")
    @PostMapping(value = "/getVenueSession")
    @LogAnnotation(operationType = "3", operationContent = "获取调整场地节次列表")
    public Result<PageInfo<VenueSessionVo>> getVenueSession(@ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                            @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize,
                                                            @ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                            @ApiParam(value = "调整周次 1表示待调整周次", example = "101010101", required = true) @RequestParam String weeks,
                                                            @ApiParam(value = "节次课时编码，多节次逗号分割", example = "0102,0103", required = false) @RequestParam(required = false) String timeCodes,
                                                            @ApiParam(value = "楼宇id", required = false) @RequestParam(required = false) Integer buildingId,
                                                            @ApiParam(value = "教室id", required = false) @RequestParam(required = false) Long roomId,
                                                            @ApiParam(value = "搜索关键词", required = false) @RequestParam(required = false) String keyword,
                                                            @ApiParam(value = "教学班相关信息", required = true) @RequestBody VenueSessionInput venueSessionInput) throws CapacityException {
        TaskInfo taskInfo = taskInfoService.getById(taskId);
        //全校统一排课 校区id置为null获取全部
        if (taskInfo.getUnifiedCampus().equals(1)) {
            venueSessionInput.setCampusId(null);
        }
        ArrangeTeachingClassDto teachingClass = BeanUtil.copyProperties(venueSessionInput, ArrangeTeachingClassDto.class);
        teachingClass.setProhibitTimeslotCodes("");
        return Result.ofSuccess(scheduleService.getVenueSession(currentPage, pageSize, taskId, weeks, timeCodes,buildingId, roomId, keyword, teachingClass));
    }

    @ApiOperation("更新场地节次")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/updateVenueSession")
    @LogAnnotation(operationType = "2", operationContent = "更新场地节次")
    public Result<List<ScheduleDto>> updateVenueSession(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                        @ApiParam(value = "学校Id", required = true) @RequestParam String schoolId,
                                                        @ApiParam(value = "学期", required = true) @RequestParam String term,
                                                        @ApiParam(value = "调整至课表", required = true) @RequestBody UpdateVenueSessionInput updateVenueSessionInput) {
        List<ScheduleDto> scheduleDtos = scheduleService.updateVenueSession(updateVenueSessionInput.getStartSchedule(), updateVenueSessionInput.getStopSchedule(),
                updateVenueSessionInput.getTeachingClassId(),updateVenueSessionInput.getHourType());
        //更新冲突表 排课状态
        arrangeCourseService.manualAdjustmentUpdate(schoolId, term, taskId, null, updateVenueSessionInput.getTeachingClassId());
        return Result.ofSuccess(scheduleDtos);
    }

    @ApiOperation("导出课程编排教学班")
    @GetMapping(value = "/excel/course")
    @LogAnnotation(operationType = "14", operationContent = "导出课程编排教学班")
    public Result<String> courseToExcel(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                        @ApiParam(value = "排课状态 1已排（正常）、2已排（未满足规则）、3已排（冲突）、4 待排", required = false) @RequestParam(required = false) Integer scheduleState,
                                        @ApiParam(value = "年级", required = false) @RequestParam(required = false) String gradeId,
                                        @ApiParam(value = "年级", required = false) @RequestParam(required = false) String gradeName,
                                        @ApiParam(value = "专业", required = false) @RequestParam(required = false) String majorId,
                                        @ApiParam(value = "专业", required = false) @RequestParam(required = false) String majorName,
                                        @ApiParam(value = "搜索关键字", required = false) @RequestParam(required = false) String keyword) {
        return Result.ofSuccess(scheduleService.courseToExcel(taskId, scheduleState, gradeId, gradeName, majorId, majorName, keyword));
    }


}
